<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称"/>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.hoscode" placeholder="医院编号"/>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getPageList()">查询</el-button>
    </el-form>

    <!-- 工具条 -->
    <div>
      <el-button type="danger" size="mini" @click="removeRows()">批量删除</el-button>
    </div>

    <!-- banner列表 -->
    <el-table
      :data="list"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >

      <!--复选框-->
      <el-table-column type="selection" width="55"/>
      <el-table-column type="index" width="50" label="序号"/>
      <el-table-column prop="hosname" label="医院名称"/>
      <el-table-column prop="hoscode" label="医院编号"/>
      <el-table-column prop="apiUrl" label="api基础路径" width="200"/>
      <el-table-column prop="contactsName" label="联系人姓名"/>
      <el-table-column prop="contactsPhone" label="联系人手机"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? '可用' : '不可用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini"
                     icon="el-icon-delete" @click="removeDataById(scope.row.id)"></el-button>

          <el-button type="text"
                     @click="lockHospSet(
                       scope.row.id,
                       scope.row.status === 1 ? 0 : 1
                     )"
          >
            {{ scope.row.status === 1 ? '锁定' : '取消锁定' }}
          </el-button>
          <el-button type="text" @click="goToUpdate(scope.row)">修改</el-button>

        </template>
      </el-table-column>

    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getPageList"
    />

  </div>

</template>

<script>
import {getPageListApi, removeDataByIdApi, removeRowsApi, lockHospSetApi} from '../../api/hospSet'

export default {
  name: "list",
  data() {
    return {
      current: 1,
      limit: 5,
      searchObj: {
        hosname: '',
        hoscode: ''
      },
      total: 0,
      list: [],
      delIds: [],
    }
  },
  methods: {
    //获取列表信息
    async getPageList(page = 1) {
      this.current = page

      let {data} = await getPageListApi(this.current, this.limit, this.searchObj)
      this.list = data.records
      this.total = data.total
      console.log(data)
    },
    //删除
    removeDataById(id) {
      this.$confirm('此操作将永久删除医院是设置信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async response => {
        //点击确定，执行下方代码
        let {data} = await removeDataByIdApi(id)
        this.$message.success('删除成功')
        this.getPageList(this.current)
      })

    },
    //复选框发生改变后出发
    handleSelectionChange(val) {
      this.delIds = val.map(item => {
        return item.id
      })
    },
    //点击批量删除按钮时出发
    removeRows() {
      this.$confirm('此操作将永久删除医院是设置信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async response => {
        //这里有个坑，哎，前端太难了
        //如果我直接调用removeRowsApi(this.delIds)的话，他不按照顺序来执行.就是先走了下面的getPageList然后再删除，这不扯淡吗
        await removeRowsApi(this.delIds)
        this.$message.success('删除成功')
        this.getPageList(1)
      })
    },
    //锁定和取消锁定
    async lockHospSet(id, status) {
      await lockHospSetApi(id, status)
      this.$message.success('修改成功')
      this.getPageList(1)
    },
    //跳转至修改页面
    goToUpdate(info) {
      delete info.param
      this.$router.push({
        name:'AddHospSet',
        params:{
          info
        }
      })
    }


  },
  mounted() {
    this.getPageList()
  }
}
</script>

<style scoped>

</style>
